using System;
using System.Collections.Generic;
using System.Text;

namespace NullableTypes
{
  #region Database reader type
  class DatabaseReader
  {
    // Nullable data field.
    public int? numericValue = null;
    public bool? boolValue = true;

    // Note the nullable return type. 
    public int? GetIntFromDatabase()
    { return numericValue; }

    // Note the nullable return type. 
    public bool? GetBoolFromDatabase()
    { return boolValue; }
  }
  #endregion 

  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("***** Fun with Nullable Data *****\n");
      DatabaseReader dr = new DatabaseReader();

      // Get int from 'database'.
      int? i = dr.GetIntFromDatabase();
      if (i.HasValue)
        Console.WriteLine("Value of 'i' is: {0}", i.Value);
      else
        Console.WriteLine("Value of 'i' is undefined.");

      // Get bool from 'database'.
      bool? b = dr.GetBoolFromDatabase();
      if (b != null)
        Console.WriteLine("Value of 'b' is: {0}", b.Value);
      else
        Console.WriteLine("Value of 'b' is undefined.");

      // If the value from GetIntFromDatabase() is null, 
      // assign local variable to 100.
      int? myData = dr.GetIntFromDatabase() ?? 100;
      Console.WriteLine("Value of myData: {0}", myData.Value);

      Console.ReadLine();
    }

    #region Declaration of local vars.
    static void LocalNullableVariables()
    {
      // Define some local nullable types. 
      //int? nullableInt = 10;
      //double? nullableDouble = 3.14;
      //bool? nullableBool = null;
      //char? nullableChar = 'a';
      //int?[] arrayOfNullableInts = new int?[10];

      // Define some local nullable types using Nullable<T>. 
      Nullable<int> nullableInt = 10;
      Nullable<double> nullableDouble = 3.14;
      Nullable<bool> nullableBool = null;
      Nullable<char> nullableChar = 'a';
      Nullable<int>[] arrayOfNullableInts = new int?[10];
    }
    #endregion
  }
}
